package com.laoyao.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.laoyao.util.TextUtil;
import com.laoyao.util.WordUtil;

public class VideoService {
	public static final VideoService me = Duang.duang(VideoService.class);
	private static VideoService dao = Duang.duang(VideoService.class);
	/**
	 * 获取一级分类视频
	 * @param id
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public Page<Record> getVideoByType1(String id,Integer pageNumber,Integer pageSize){
		String sql  = "select * from v_video where type1Id=? order by id desc";
		return Db.paginate(pageNumber, pageSize, sql,id);
	}
	/**
	 * 获取二级分类视频
	 * @param type1Id
	 * @param type2Id
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public  Page<Record> getVideoByType(String type1Id,String type2Id ,Integer pageNumber,Integer pageSize){
		String sql = "select a.*,b.userName from v_video a,v_user b where a.userId=b.id and type1Id=?  and a.state=1 and checkState=1 ";
		List<String> list = new ArrayList();
		list.add(type1Id);
		if(!TextUtil.isEmpty(type2Id)){
			sql+=" and  type2Id=? ";
			list.add(type2Id);
		}
		sql+="order by id desc";
		
		return Db.paginate(pageNumber, pageSize, sql,list.toArray());
	}
	
	/**
	 * 获取视频点赞数
	 * @param id
	 * @return
	 */
	public Long getPraiseNum(String id){
		String sql = "select count(*) num from v_praise where videoId=?";
		return Db.findFirst(sql,id).getLong("num");
	}
	/**
	 * 是否点赞过
	 * @param userId
	 * @param videoId
	 * @return
	 */
	
	public boolean isExistPraise(String userId,String videoId){
		String sql = "select * from v_praise where userId=? and videoId=?";
		return Db.findFirst(sql,userId,videoId)!=null;
	}
	
	/**
	 * 是否存在过观看历史
	 * @param userId
	 * @param videoId
	 * @return
	 */
	public boolean isExistHistory(Integer userId,Integer videoId){
		String sql = "select * from v_history where userId=? and videoId=?";
		return Db.findFirst(sql,userId,videoId)!=null;
	}
	
	/**
	 * 观看历史列表
	 * @param pageNumber
	 * @param pageSize
	 * @param id
	 * @return
	 */
	public Page<Record> historyList(Integer pageNumber,Integer pageSize,String id){
		String sql = "select a.*,b.userName,c.time  from v_video a,v_user b,v_history c where a.userId=b.id and c.videoId=a.id and c.userId=?"
				+ " order by time desc";
		return Db.paginate(pageNumber, pageSize, sql,id);
	}
    /**
     * 获取我投稿视频的列表
     * @param pageNumber
     * @param pageSize
     * @param id 用户id
     * @return
     */
	public Page<Record> getMyVideo(Integer pageNumber,Integer pageSize ,String id){
		String sql = "select * from v_video where userId=? and state=1 order by id desc";
		return Db.paginate(pageNumber, pageSize, sql,id);
	}
	public Page<Record> getMyAttention(Integer pageNumber,Integer pageSize ,String id){
		String sql = "select a.*,b.userName from v_video a,v_user b,v_fans c where c.rid=a.userId and a.userId=b.id and c.uid=? and a.state=1"
				+ " order by uploadTime desc";
		
		return Db.paginate(pageNumber, pageSize, sql,id);
	}
	/**
	 * 视频列表
	 * @param pageNumber
	 * @param pageSize
	 * @param title 视频标题
	 * @return
	 */
	
	public Page<Record> list(Integer pageNumber,Integer pageSize ,String title,String isVip,String type1Id,String type2Id){
		String sql = "select a.*,b.userName from v_video a,v_user b where a.userId=b.id ";
		List<Object> list = new ArrayList<Object>();
		if(!TextUtil.isEmpty(title)){
			sql+=" and title like ?";
			list.add(title+"%");
		}
		
		if(!TextUtil.isEmpty(isVip)){
			sql+=" and isVip=?";
			list.add(isVip);
		}
		
		if(!TextUtil.isEmpty(type1Id)){
			sql+=" and type1Id=?";
			list.add(type1Id);
		}
		
		if(!TextUtil.isEmpty(type2Id)){
			sql+=" and type2Id=?";

			list.add(type2Id);
		}
		
		sql+=" order by id desc";
		return Db.paginate(pageNumber, pageSize, sql,list.toArray());
	}
	
	public Page<Record> rankings(Integer pageNumber,Integer pageSize,String type1Id,String type2Id){
		String sql = "select * from v_video where 1=1";
		
		List<Object> list = new ArrayList<Object>();
		
		if(!TextUtil.isEmpty(type1Id)){
			sql+=" and type1Id=?";
			list.add(type1Id);
		}
		
		if(!TextUtil.isEmpty(type2Id)){
			sql+=" and type2Id=?";
			list.add(type2Id);
		}
		
		sql+=" order by playNum desc";
		return Db.paginate(pageNumber, pageSize, sql,list.toArray());
	}
	
	public Page<Record> top(Integer pageNumber,Integer pageSize){
		//返回近1000条数据播放量的前n条数据
		String sql = "select v.*,b.userName from  (select * from v_video ORDER BY id desc LIMIT 0,1000) v ,v_user b "
				+ "where v.userId=b.id order by playNum desc";
		return Db.paginate(pageNumber, pageSize, sql);
	}
	
	public Page<Record> relate(Integer pageNumber,Integer pageSize,String text,String type1Id,String type2Id){
		String sql = "select a.*,b.userName from ( select * from v_video where id='' ";
		
		//先获取标题分词
		List<String> list = null;
		try {
			list = WordUtil.word(text);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//根据标题分词组装sql语句
		if(list!=null){
			for(int i=0;i<list.size();i++){
				sql+=" union select * from v_video where title like ? ";
				if(!TextUtil.isEmpty(list.get(i))){
					list.set(i, "%"+list.get(i)+"%");
				}
			}
		}
		sql+=" union select * from v_video where type1Id=? and type2Id=? ) a,v_user b where a.userId=b.id";
		
		list.add(type1Id);
		list.add(type2Id);	
		System.out.println(sql+Db.paginate(pageNumber, pageSize, sql,list.toArray()));
		return Db.paginate(pageNumber, pageSize, sql,list.toArray());
	}
	
	

}
